package controller 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import model.GameData;
	import model.MyData;
	import request.HTTPRequest;
	/**
	 * ...
	 * @author bimkute
	 */
	public class RequestController extends EventDispatcher
	{
		private static var instance:RequestController;
		private var httpRequest:HTTPRequest;
		
		public function RequestController() 
		{
			httpRequest = new HTTPRequest();
			
		}
		
		public static function getInstance():RequestController 
		{
			if (!instance) 
			{
				instance = new RequestController();
			}
			return instance;
		}
		
		public function sendDataBetting(obj:Object):void 
		{
			var gameID:int = obj[Const.SEND_GAME_ID];
			var arr:Array = obj[Const.BET];

			for (var i:int = 0; i < arr.length; i++) 
			{
				if (arr[i] == "Nhập sao")
				{
					arr[i] = 0;
				}
			}

			MyData.getInstance()._arrBetting = arr;
			var userName:String = obj[Const.PLAYER_NAME];
			//var userName:String = "test2";
			
			var str:String = "http://ngutruong.letshare.vn/api/bet/gameId/" + String(gameID) + "/player/" + userName + 
								"/bet/" + arr;
			
			httpRequest.sendRequest("POST", str, obj, sendDataBetComplete, true);
		}
		
		private function sendDataBetComplete(value:Object):void 
		{
			if (int(value[Const.ERROR]) == 0 && GameData.getInstance().idGame == value[Const.GAME_ID]) 
			{
				
				MyData.getInstance().myMoney = value[Const.BALANCE];
				
				
				dispatchEvent(new Event("send_complete"));
			}
			else 
			{
				dispatchEvent(new Event("send_error"));
			}
		}
		
		public function getInfo(getResult:Boolean = false):void 
		{
			var obj:Object = new Object();
			
			var str:String = "http://ngutruong.letshare.vn/api/info/player/" + MyData.getInstance().myName;
			
			var httpRequest1:HTTPRequest = new HTTPRequest();
			
			obj["userName"] = MyData.getInstance().myName;
			
			if (getResult) 
			{
				
				httpRequest1.sendRequest("POST", str, obj, checkGetResult, true);
			}
			else 
			{
				httpRequest1.sendRequest("POST", str, obj, getDataMatchComplete, true);
			}
			
		}
		
		private function checkGetResult(value:Object):void 
		{
			
			if (value[Const.GAME_STATUS] == 3) 
			{
				GameData.getInstance().canGetResult = true;
			}
			else 
			{
				GameData.getInstance().canGetResult = false;
			}
		}
		
		private function getDataMatchComplete(value:Object):void 
		{
			trace("=-==-=-==-: ", value[Const.ERROR], MyData.getInstance()._getDataMatchComplete)
			if (int(value[Const.ERROR]) == 0 && !MyData.getInstance()._getDataMatchComplete) // && GameData.getInstance().idGame != value[Const.GAME_ID]
			{
				if (value[Const.GAME_STATUS] == 3) 
				{
					trace("**********: ", value[Const.GAME_ID], "---====----", MyData.getInstance().canStartGame)
					GameData.getInstance().idGame = int(value[Const.GAME_ID]);
					GameData.getInstance().canGetResult = true;
					
					if (MyData.getInstance().canStartGame && GameData.getInstance().fistTime) 
					{
						
						//
						var obj:Object = new Object();
						obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
						obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
						
						getResult(obj);
					}
					else 
					{
						dispatchEvent(new Event("startAgain"));
					}
				}
				else 
				{
					trace("eleses########: ", value[Const.GAME_ID], "---====----", value[Const.BALANCE])
					trace("!!!!!eleses########: ", value[Const.END_TIME], value[Const.CUREENT_TIME], "---====----", value[Const.TOTAL_MONEY])
					GameData.getInstance().idGame = int(value[Const.GAME_ID]);
					if (value[Const.BALANCE])
					{
						MyData.getInstance().myMoney = value[Const.BALANCE];
					}
					
					
					GameData.getInstance().multi = value;
					if (GameData.getInstance().totalMoneyToday != int(value[Const.TOTAL_MONEY])) 
					{
						GameData.getInstance().totalMoneyToday = int(value[Const.TOTAL_MONEY]);
					}
					
					GameData.getInstance().timeRemain = int(value[Const.END_TIME]) - int(value[Const.CUREENT_TIME]);
					
					dispatchEvent(new Event("call_complete"));
				}
				
			}
			else 
			{
				dispatchEvent(new Event("call_error"));
			}
		}
		
		public function getResult(obj:Object):void 
		{
			
			var gameID:int = obj[Const.SEND_GAME_ID];
			var userName:String = obj[Const.PLAYER_NAME];
			//var userName:String = "test2";
			var objs:Object = new Object();
			
			var str:String = "http://ngutruong.letshare.vn/api/result/gameId/" + gameID + "/player/" + userName;
			
			//httpRequest.sendRequest("GET", str, objs, getDataResultComplete, true);
			var httpRequest2:HTTPRequest = new HTTPRequest();
			httpRequest2.sendRequest("GET", str, objs, getDataResultComplete, true);
		}
		
		private function getDataResultComplete(value:Object):void 
		{
			
			if ((value[Const.ERROR]) && (int(value[Const.ERROR]) == 0 && 
					GameData.getInstance().idGame == value[Const.GAME_ID] && 
					!GameData.getInstance()._checkHaveResult) || 
					(int(value[Const.ERROR]) == 0 && GameData.getInstance().fistTime))
			{
				
				if (GameData.getInstance().timeRemain <= 0) 
				{
					MyData.getInstance()._getDataMatchComplete = true;
				}
				
				if (value[Const.TOTAL_BET])
				{
					GameData.getInstance().totalBet = value[Const.TOTAL_BET];
				}
				if (value[Const.TOTAL_WIN])
				{
					GameData.getInstance().totalWin = value[Const.TOTAL_WIN];
				}
				if (value[Const.RESULT])
				{
					GameData.getInstance().result = value[Const.RESULT].split(",");
				}
				GameData.getInstance().idGame = value[Const.GAME_ID];
				
				GameData.getInstance()._checkHaveResult = true;
				GameData.getInstance().fistTime = false;
				dispatchEvent(new Event("getResultComplete"));
				
				trace("DISPATCH EVENT");
			}
			else 
			{
				
			}
			
		}
		
		public function sendDataHistory(obj:Object):void 
		{
			
			var gameID:int = obj[Const.SEND_GAME_ID];
			var userName:String = obj[Const.PLAYER_NAME];
			//var userName:String = "test2";
			var objs:Object = new Object();
			
			var str:String = "http://ngutruong.letshare.vn/api/transaction/player/" + userName + "/page/" + String(MyData.getInstance().page) + "/pageSize/5";
			
			//httpRequest.sendRequest("GET", str, objs, getDataResultComplete, true);
			var httpRequest3:HTTPRequest = new HTTPRequest();
			httpRequest3.sendRequest("GET", str, objs, getDataHistoryComplete, true);
		}
		
		private function getDataHistoryComplete(valueHistory:Object):void 
		{
			var i :int;
			if (int(valueHistory[Const.ERROR]) == 0)
			{
				if (GameData.getInstance().arrTransaction && GameData.getInstance().arrTransaction.length > 0)
				{
					for (i = 0; i < GameData.getInstance().arrTransaction.length; i++) 
					{
						GameData.getInstance().arrTransaction[i] = null;
					}
					GameData.getInstance().arrTransaction = [];
				}
				
				GameData.getInstance().arrTransaction = valueHistory[Const.TRANSACTION];
				MyData.getInstance().page = valueHistory[Const.PAGE];
				dispatchEvent(new Event("getHistoryComplete"))
			}
		}
	}

}